package com.skyolder.milkbee.service;

import com.skyolder.milkbee.domain.Present;
import com.skyolder.milkbee.repository.PresentRepository;
import com.skyolder.milkbee.service.dto.PresentDTO;
import com.skyolder.milkbee.service.mapper.PresentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Present.
 */
@Service
@Transactional
public class PresentService {

    private final Logger log = LoggerFactory.getLogger(PresentService.class);
    
    @Inject
    private PresentRepository presentRepository;

    @Inject
    private PresentMapper presentMapper;

    /**
     * Save a present.
     *
     * @param presentDTO the entity to save
     * @return the persisted entity
     */
    public PresentDTO save(PresentDTO presentDTO) {
        log.debug("Request to save Present : {}", presentDTO);
        Present present = presentMapper.presentDTOToPresent(presentDTO);
        present = presentRepository.save(present);
        PresentDTO result = presentMapper.presentToPresentDTO(present);
        return result;
    }

    /**
     *  Get all the presents.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<PresentDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Presents");
        Page<Present> result = presentRepository.findAll(pageable);
        return result.map(present -> presentMapper.presentToPresentDTO(present));
    }

    /**
     *  Get one present by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public PresentDTO findOne(Long id) {
        log.debug("Request to get Present : {}", id);
        Present present = presentRepository.findOne(id);
        PresentDTO presentDTO = presentMapper.presentToPresentDTO(present);
        return presentDTO;
    }

    /**
     *  Delete the  present by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Present : {}", id);
        presentRepository.delete(id);
    }


	public Page<PresentDTO> findByRecieverId(Long userId, Pageable pageable) {
		Page<Present> result = presentRepository.findByRecieverId(userId,pageable);
        return result.map(present -> presentMapper.presentToPresentDTO(present));
	}
}
